package com.example.dell.wisdom_farm;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.view.LineChartView;

public class view3 extends Fragment {
    public String[] spinnerData=new String[]{"空气温度","土壤温度","CO₂浓度","光照强度"};
    public String[] spinnerData1=new String[]{"1分钟","2分钟","3分钟","4分钟","5分钟"};

    private Button button1;
    Spinner spinner1;
    Spinner spinner2;
    LineChartView lineChartView;


        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup,Bundle bundle){
            View view=inflater.inflate(R.layout.view3,viewGroup,false);
            lineChartView=(LineChartView)view.findViewById(R.id.view_3_lecho);
            button1=(Button)view.findViewById(R.id.view_3_button);
            spinner1=(Spinner)view.findViewById(R.id.view_3_spinner1);
            spinner2=(Spinner)view.findViewById(R.id.view_3_spinner2);
            spinner1.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,spinnerData));
            spinner2.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,spinnerData1));
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showChart();
                }
            });
            return view;
        }

        public void showChart(){
            List<PointValue> pointValues=new ArrayList<>();
            pointValues.add(new PointValue(0,30));
            pointValues.add(new PointValue(1,20));
            pointValues.add(new PointValue(2,10));
            pointValues.add(new PointValue(3,40));
            pointValues.add(new PointValue(4,27));
            pointValues.add(new PointValue(5,40));
            pointValues.add(new PointValue(6,35));
            pointValues.add(new PointValue(7,20));


            Line line1=new Line(pointValues).setColor(Color.GRAY);
            List<Line> lines=new ArrayList<>();
            lines.add(line1);
            LineChartData data=new LineChartData();
            data.setLines(lines);

            int[] num=new int[]{0,5,10,15,20,25,30,35,40,45,50,55};
            List<AxisValue> Y=new ArrayList<>();
            for(int i=0;i<num.length;i++)
            {
                Y.add(new AxisValue(i).setValue(num[i]));
            }
            String[] str2=new String[]{"一小时","二小时","三小时","四小时","五小时","六小时","七小时","八小时"};
            List<AxisValue> X=new ArrayList<>();
            for(int i=0;i<str2.length;i++) {
                X.add(new AxisValue(i).setLabel(str2[i]));
            }

            //设置坐标名称
            Axis axisX=new Axis();
            Axis axisY=new Axis();



            axisX.setName("时间");
            axisY.setName("数值");



            data.setAxisXBottom(axisX);
            data.setAxisYLeft(axisY);

            axisX.setValues(X);
            axisY.setValues(Y);


            lineChartView.setLineChartData(data);


        }
}
